package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.controller.util.Code;
import com.example.controller.util.Data;
import com.example.controller.util.UserHolder;
import com.example.domain.Activity;
import com.example.domain.Community;
import com.example.service.IActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activitys")
public class ActivityController {
    @Autowired
    private IActivityService activityService;

//    分页查询所有活动
    @GetMapping("/{pageCurrent}/{pageSize}")
    public Data getAll(@PathVariable int pageCurrent,@PathVariable int pageSize){
//        List<Activity> list = activityService.list();
        //查询数据库
        IPage<Activity> activity = activityService.getActivity(pageCurrent, pageSize);
        Integer code = activity!=null ? Code.Code_ok : Code.Code_err;
        String msg = activity != null ? "querry_ok" : "querry_err";
         return  new Data(code,activity,msg);
    }

//    删除活动
    @DeleteMapping("/{activityId}")
    public Data delete(@PathVariable int activityId){
        boolean b = activityService.removeById(activityId);
        Integer code = b ? Code.Code_ok : Code.Code_err;
        String msg = b ? "删除成功" : "删除失败";
        return new Data(code,msg);
    }

//    查询活动关键字,和活动的种类
    @GetMapping("/like/{name}")
    public Data getKeyWord( @PathVariable String name){
        List keyWord = activityService.getKeyWord(name);
        Integer code = keyWord != null ? Code.Code_ok : Code.Code_err;
        String msg = keyWord != null ?  "querry_ok"  : " querry_err";
        return new Data(code,keyWord,msg);
    }
    @GetMapping("/{id}")
    public Data getById(@PathVariable Integer id){
        LambdaQueryWrapper<Activity> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Activity::getActivityId,id);
        Activity activity = activityService.getOne(queryWrapper);
        Integer code = activity != null ? Code.Code_ok : Code.Code_err;
        String msg = activity != null ? "查询成功" : "查询失败";
        return new Data(code, activity, msg);
    }
//    查询所有活动
    @GetMapping
    public Data getAll(){
        List<Activity> list = activityService.list();
        Integer code = list!=null ? Code.Code_ok : Code.Code_err;
        String msg = list != null ? "querry_ok" : "querry_err";
        return new Data(code,list,msg);
    }
//    添加活动
    @PostMapping
    public Data saveActivity(@RequestBody Activity activity){
        boolean save = activityService.save(activity);
//        Integer activityId = activity.getId();
//        Integer volunteerId =
        Integer code = save ? Code.Code_ok : Code.Code_err;
        String msg = save ? "添加成功" : "添加失败";
        return new Data(code,msg);
    }

//    更新活动
    @PutMapping
    public Data updateById(@RequestBody Activity activity){
        boolean b = activityService.updateById(activity);
        Integer code = b==true ? Code.Code_ok : Code.Code_err;
        String msg = b==true ? "更新成功" : "更新失败";
        return new Data(code,msg);
    }
}
